import networkx as nx
import numpy as np
import glob
import os, os.path
import math
import pickle
import scipy.sparse as sp

pathhack = os.path.dirname(os.path.realpath(__file__))

network = nx.Graph()

FILE_NAME = 'Yale'

def load_edges():
    global network
    edge_file = open("./original/{}.txt".format(FILE_NAME),"r")
    for line in edge_file:
        # nodefrom nodeto
        split = [x for x in line.split(" ")]
        node_from = int(split[0])
        node_to = int(split[1])
        network.add_edge(node_from, node_to)


if __name__ == '__main__':
    # print "Running tests."
    print("Loading network...")
    load_edges()
    print("done.")

    failures = 0

    def test(actual, expected, test_name):
        global failures  # lol python scope
        try:
            print("testing %s..." % (test_name,))
            assert actual == expected, "%s failed (%s != %s)!" % (test_name, actual, expected)
            print("%s passed (%s == %s)." % (test_name, actual, expected))
        except AssertionError as e:
            print(e)
            failures += 1


    print('network size', network.size())
    print("%d tests failed." % (failures,))

    print('')
    print("Saving network...")
    adj = nx.adjacency_matrix(network)
    print(adj.shape[0])

    with open("./processed/{}-adj.pkl".format(FILE_NAME), "wb") as f:
        pickle.dump(adj, f)

    print("Network saved!")

